<?php

class Admin_reportController extends Admin_baseController {
    public function customerAction(){
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);
        $where = $value = $url = array();
        $name = Request::getParams('name');
        $uid = Request::getParams('uid');
        $time = Request::getParams('time');
        if($name){
            $where[]="name like ?";
            $value[]='%'.$name.'%';
            $url['name']=$name;
        }
        if($uid){
            $where[]="id=?";
            $value[]=$uid;
            $url['uid']=$uid;
        }

        $startTime = $endTime = '';
        if ($time) {
            $url['time'] = $time;
            list($startTime, $endTime) = explode(' - ', $time);
        }
        if ($startTime) {
            $where[] = 'ctime>=?';
            $value[] = $startTime;
            $url['startTime'] = $startTime;
        }
        if ($endTime) {
            $where[] = 'ctime<=?';
            $value[] = $endTime;
            $url['endTime'] = $endTime;
        }

        $total = self::model('dealer')->count(null,$where, $value);
        $list = self::model('dealer')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);
        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'name' => $name,
            'uid' => $uid,
            'time' => $time,
            'list' => $list,
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_report/customer?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
        ));
        $this->display();
    }
    public function customerInfoAction(){
        $ids=Request::getParams('ids');
        $info=self::model("dealer")->find($ids);
        $this->assign([
            'info'=>$info
        ]);
        $this->display();
    }

    public function goodsAction(){
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);
        $where = $value = $url = array();
        $total = self::model('goods')->count(null, $where, $value);
        $list = self::model('goods')->limit(($page - 1) * $pageSize, $pageSize)->order('goods_id desc')->getAll($where, $value);
        foreach ($list as $k=>$v){
            $sql="select sum(num) as znum,sum(num*price) as zprice from wf_order_goods where pay_status=0 and goods_id=".$v['goods_id'];
            $sales=self::model("goods")->query($sql)->fetchAll();
            $list[$k]['sales_num']=empty($sales[0]['znum'])?0:$sales[0]['znum'];
            $list[$k]['sales_price']=empty($sales[0]['zprice'])?0:$sales[0]['zprice'];
        }
        $this->assign([
            'page' => $page,
            'pageSize' => $pageSize,
            'list' => $list,
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_dealer/income?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
        ]);
        $this->display();
    }

	public function listAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$reason = Request::getParams('reason');
		$where[] = 'status=0';
		if ($reason) {
			$where[] = 'reason like ?';
			$value[] = '%' . $reason . '%';
			$url['reason'] = $reason;
		}
		$total = self::model('report')->count(null, $where, $value);
		$list = self::model('report')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);
		$ids = [];
		foreach ($list as $key => $value) {
			$ids[] = $value['dynamic_id'];
			$list[$key]['dynamic_title'] = '';
		}
		if ($ids) {
			$ids = implode(',', $ids);
			$dynamicList = self::model('dynamic')->query("SELECT dynamic_id,title FROM wf_dynamic WHERE dynamic_id in  ({$ids})")->fetchAll();
			foreach ($list as $key1 => $value1) {
				foreach ($dynamicList as $key2 => $value2) {
					if ($value1['dynamic_id'] == $value2['dynamic_id']) {
						$list[$key1]['dynamic_title'] = $value2['title'];
					}
				}
			}
		}

		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'reason' => $reason,
			'total' => $total,
			'reasonList' => self::model('report_reason')->getListWithIndex(),
			'list' => $list,
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_report/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	public function deleteAction() {
		$id = intval(Request::getParams('reportId'));
		if (self::model('report')->delete('id=?', $id)) {
			Response::ajax(200, '删除成功');
		}
		Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
	}

	public function sendMsgAction() {
		$id = intval(Request::getParams('reportId'));
		if (Request::isGet()) {
			$this->assign('reportId', $id);
			$this->display();
			exit;
		}
		$content = Request::getParams('content');
		if (!$content) {
			Response::ajax(NORMAL_ERROR, '内容不能为空');
		}
		$reportInfo = self::model('report')->getOne('id=?', $id);
		if (!$reportInfo) {
			Response::ajax(NORMAL_ERROR, '举报信息不存在');
		}
		if ($reportInfo['status']) {
			Response::ajax(NORMAL_ERROR, '举报信息已处理');
		}
		if (self::model('report')->update([
			'status' => 1,
		], 'id=?', $id)) {
			self::model('user_notify')->addNotify($reportInfo['uid'], notifyModel::TYPE_NOTICE,
				$reportInfo['dynamic_id'], reportModel::$reportUser, $content);
			Response::ajax(200, '处理成功');
		}
		Response::ajax(NORMAL_ERROR, '处理失败,请稍后再试');
	}

	public function reasonListAction() {
		$this->assign([
			'list' => self::model('report_reason')->order('sort asc')->getAll(),
		]);
		$this->display();
	}

	public function reasonEditAction() {
		$id = Request::getParams('reasonId');
		$data = [];
		$data['title'] = Request::getParams('title');
		$data['sort'] = intval(Request::getParams('sort'));

		$info = [];
		if ($id && !$info = self::model('report_reason')->getOne('reason_id=?', $id)) {
			Response::showMsg('信息查询失败，请稍后重试');
		}
		if (Request::isGet()) {
			$this->assign('info', $info);
			$this->display();
			exit;
		}
//        $result = report_reasonModel::validata($data);
		//        if($result!==true) {
		//            Response::ajax(NORMAL_ERROR, $result);
		//        }
		if ($id) {
//修改
			if (self::model('report_reason')->update($data, 'reason_id=?', $id)) {
				Response::ajax(200, '修改成功');
			}
		} else {
//新增
			if (self::model('report_reason')->insert($data)) {
				Response::ajax(200, '添加成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function reasonDeleteAction() {
		$id = intval(Request::getParams('reasonId'));
		if (self::model('report_reason')->delete('reason_id=?', $id)) {
			Response::ajax(200, '删除成功');
		}
		Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
	}

}